package com.map.code;

/*
 * 百
 * 度
 * 地
 * 图
 * 导
 * 航
 * 设
 * 置
 * */
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.baidu.navisdk.adapter.BNOuterLogUtil;
import com.baidu.navisdk.adapter.BNOuterTTSPlayerCallback;
import com.baidu.navisdk.adapter.BNRouteGuideManager;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BNRoutePlanNode.CoordinateType;
import com.baidu.navisdk.adapter.BNaviSettingManager;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.baidu.navisdk.adapter.BaiduNaviManager.NaviInitListener;
import com.baidu.navisdk.adapter.BaiduNaviManager.RoutePlanListener;
import com.baidu.navisdk.adapter.PackageUtil;
import com.baidu.navisdk.adapter.base.BaiduNaviSDKProxy;
import com.example.android_familyposition.MainActivity;
import com.example.android_familyposition.R;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class BNDemoMainActivity extends Activity {

	public static List<Activity> activityList = new LinkedList<Activity>();

	private static final String APP_FOLDER_NAME = "BNSDKSimpleDemo";

	/*private Button mWgsNaviBtn = null;
	private Button mGcjNaviBtn = null;
	private Button mBdmcNaviBtn = null;
	private Button mDb06ll = null;*/
	private String mSDCardPath = null;
	TextView tx_loading;

	public static final String ROUTE_PLAN_NODE = "routePlanNode";
	public static final String SHOW_CUSTOM_ITEM = "showCustomItem";
	public static final String RESET_END_NODE = "resetEndNode";
	public static final String VOID_MODE = "voidMode";

	private final static String authBaseArr[] = {
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.ACCESS_FINE_LOCATION };
	private final static String authComArr[] = { Manifest.permission.READ_PHONE_STATE };
	private final static int authBaseRequestCode = 1;
	private final static int authComRequestCode = 2;

	private boolean hasInitSuccess = false;
	private boolean hasRequestComAuth = false;
	
	private CoordinateType mCoordinateType = null;
	BNRoutePlanNode sNode = null;
	BNRoutePlanNode eNode = null;
	
	Boolean InitSuccess=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		activityList.add(this);

		setContentView(R.layout.activity_main2);
		
		tx_loading=(TextView)findViewById(R.id.tx_loading);
		/*mWgsNaviBtn = (Button) findViewById(R.id.wgsNaviBtn);
		mGcjNaviBtn = (Button) findViewById(R.id.gcjNaviBtn);
		mBdmcNaviBtn = (Button) findViewById(R.id.bdmcNaviBtn);
		mDb06ll = (Button) findViewById(R.id.mDb06llNaviBtn);
		BNOuterLogUtil.setLogSwitcher(true);*/
		
		SharedPreferences sharedPreferences = getSharedPreferences("Init", Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();//获取编辑器
		if (sharedPreferences.getInt("times", 0)==0) {
			editor.putInt("times", 2);
			editor.commit();
			InitSuccess=true;
		}else if(sharedPreferences.getInt("times", 0)!=2){
			editor.putInt("times", 2);
			editor.commit();
			InitSuccess=true;
		}else{
			editor.putInt("times", 1);
			editor.commit();
		}

		initListener();
		if (initDirs()) {
			initNavi();
		}

		// BNOuterLogUtil.setLogSwitcher(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initListener() {
		
		routeplanToNavi(CoordinateType.BD09LL);

		/*if (mWgsNaviBtn != null) {
			mWgsNaviBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (BaiduNaviManager.isNaviInited()) {
						routeplanToNavi(CoordinateType.WGS84);
						//Toast.makeText(BNDemoMainActivity.this, "1",Toast.LENGTH_SHORT).show();
					}
				}

			});
		}
		if (mGcjNaviBtn != null) {
			mGcjNaviBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (BaiduNaviManager.isNaviInited()) {
						routeplanToNavi(CoordinateType.GCJ02);
					}
				}

			});
		}
		if (mBdmcNaviBtn != null) {
			mBdmcNaviBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					if (BaiduNaviManager.isNaviInited()) {
						routeplanToNavi(CoordinateType.BD09_MC);
					}
				}
			});
		}

		if (mDb06ll != null) {
			mDb06ll.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (BaiduNaviManager.isNaviInited()) {
						routeplanToNavi(CoordinateType.BD09LL);
					}
				}
			});
		}*/

	}

	private boolean initDirs() {
		mSDCardPath = getSdcardDir();
		if (mSDCardPath == null) {
			return false;
		}
		File f = new File(mSDCardPath, APP_FOLDER_NAME);
		if (!f.exists()) {
			try {
				f.mkdir();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	String authinfo = null;

	/**
	 * 内部TTS播报状态回传handler
	 */
	private Handler ttsHandler = new Handler() {
		public void handleMessage(Message msg) {
			int type = msg.what;
			switch (type) {
			case BaiduNaviManager.TTSPlayMsgType.PLAY_START_MSG: {
				showToastMsg("Handler : TTS play start");
				break;
			}
			case BaiduNaviManager.TTSPlayMsgType.PLAY_END_MSG: {
				 showToastMsg("Handler : TTS play end");
				break;
			}
			default:
				break;
			}
		}
	};

	/**
	 * 内部TTS播报状态回调接口
	 */
	private BaiduNaviManager.TTSPlayStateListener ttsPlayStateListener = new BaiduNaviManager.TTSPlayStateListener() {

		@Override
		public void playEnd() {
			 showToastMsg("TTSPlayStateListener : TTS play end");
		}

		@Override
		public void playStart() {
			 showToastMsg("TTSPlayStateListener : TTS play start");
		}
	};

	public void showToastMsg(final String msg) {
		BNDemoMainActivity.this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(BNDemoMainActivity.this, msg, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}

	private boolean hasBasePhoneAuth() {
		// TODO Auto-generated method stub

		PackageManager pm = this.getPackageManager();
		for (String auth : authBaseArr) {
			if (pm.checkPermission(auth, this.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
				return false;
			}
		}
		return true;
	}

	private boolean hasCompletePhoneAuth() {
		// TODO Auto-generated method stub

		PackageManager pm = this.getPackageManager();
		for (String auth : authComArr) {
			if (pm.checkPermission(auth, this.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
				return false;
			}
		}
		return true;
	}

	private void initNavi() {

		BNOuterTTSPlayerCallback ttsCallback = null;

		// 申请权限
		if (android.os.Build.VERSION.SDK_INT >= 23) {

			if (!hasBasePhoneAuth()) {

				// this.requestPermissions(authBaseArr, authBaseRequestCode);
				return;

			}
		}

		BaiduNaviManager.getInstance().init(this, mSDCardPath, APP_FOLDER_NAME,
				new NaviInitListener() {
					@Override
					public void onAuthResult(int status, String msg) {
						if (0 == status) {
							authinfo = "key校验成功!";
						} else {
							authinfo = "key校验失败, " + msg;
						}
						BNDemoMainActivity.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								/*Toast.makeText(BNDemoMainActivity.this,
										authinfo, Toast.LENGTH_LONG).show();*/
								tx_loading.setText(tx_loading.getText()+authinfo+"\n");
							}
						});
					}

					public void initSuccess() {
						/*Toast.makeText(BNDemoMainActivity.this,
								"百度导航引擎初始化成功", Toast.LENGTH_SHORT).show();*/
						tx_loading.setText(tx_loading.getText()+"百度导航引擎初始化成功"+"\n");
						hasInitSuccess = true;
						initSetting();
						
						if(InitSuccess==true){
							Intent intent = new Intent(BNDemoMainActivity.this,
									BNDemoMainActivity.class);
							startActivity(intent);
							finish();
						}
					}

					public void initStart() {
						/*Toast.makeText(BNDemoMainActivity.this,
								"百度导航引擎初始化开始", Toast.LENGTH_SHORT).show();*/
						tx_loading.setText(tx_loading.getText()+"百度导航引擎初始化开始"+"\n");
					}

					public void initFailed() {
						/*Toast.makeText(BNDemoMainActivity.this,
								"百度导航引擎初始化失败", Toast.LENGTH_SHORT).show();*/
						tx_loading.setText(tx_loading.getText()+"百度导航引擎初始化失败"+"\n");
					}

				}, null, ttsHandler, ttsPlayStateListener);

	}

	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	//private CoordinateType mCoordinateType = null;

	private void routeplanToNavi(CoordinateType coType) {
		mCoordinateType = coType;
		if (!hasInitSuccess) {
			//Toast.makeText(BNDemoMainActivity.this, "还未初始化!",
					//Toast.LENGTH_SHORT).show();
			tx_loading.setText(tx_loading.getText()+"还未初始化"+"\n");
		}
		// 权限申请
		if (android.os.Build.VERSION.SDK_INT >= 23) {
			// 保证导航功能完备
			if (!hasCompletePhoneAuth()) {
				if (!hasRequestComAuth) {
					hasRequestComAuth = true;
					// this.requestPermissions(authComArr, authComRequestCode);
					return;
				} else {
					//Toast.makeText(BNDemoMainActivity.this, "没有完备的权限!",
							//Toast.LENGTH_SHORT).show();
					tx_loading.setText(tx_loading.getText()+"还没有完备的权限"+"\n");
				}
			}

		}
		BNRoutePlanNode sNode = null;
		BNRoutePlanNode eNode = null;
		/*switch (coType) {
		case GCJ02: {
			sNode = new BNRoutePlanNode(116.30142, 40.05087, "百度大厦", null,
					coType);
			eNode = new BNRoutePlanNode(116.39750, 39.90882, "北京天安门", null,
					coType);
			break;
		}
		case WGS84: {
			sNode = new BNRoutePlanNode(116.300821, 40.050969, "百度大厦", null,
					coType);
			eNode = new BNRoutePlanNode(116.397491, 39.908749, "北京天安门",
					null, coType);
			break;
		}
		case BD09_MC: {
			sNode = new BNRoutePlanNode(12947471, 4846474, "百度大厦", null,
					coType);
			eNode = new BNRoutePlanNode(12958160, 4825947, "北京天安门", null,
					coType);
			break;
		}
		case BD09LL: {
			sNode = new BNRoutePlanNode(116.30784537597782, 40.057009624099436,
					"百度大厦", null, coType);
			eNode = new BNRoutePlanNode(116.40386525193937, 39.915160800132085,
					"北京天安门", null, coType);
			break;
		}
		default:
			;
		}*/
		sNode = new BNRoutePlanNode(110.418919, 21.204129, "你爹", null,
				coType);
		eNode = new BNRoutePlanNode(110.424389, 21.214937, "儿子",
				null, coType);
		if (sNode != null && eNode != null) {
			List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
			list.add(sNode);
			list.add(eNode);
			BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true,
					new DemoRoutePlanListener(sNode));
		}
	}

	public class DemoRoutePlanListener implements RoutePlanListener {

		private BNRoutePlanNode mBNRoutePlanNode = null;

		public DemoRoutePlanListener(BNRoutePlanNode node) {
			mBNRoutePlanNode = node;
		}

		@Override
		public void onJumpToNavigator() {
			/*
			 * 设置途径点以及resetEndNode会回调该接口
			 */

			for (Activity ac : activityList) {

				if (ac.getClass().getName().endsWith("BNDemoGuideActivity")) {

					return;
				}
			}
			Intent intent = new Intent(BNDemoMainActivity.this,
					BNDemoGuideActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable(ROUTE_PLAN_NODE,
					(BNRoutePlanNode) mBNRoutePlanNode);
			intent.putExtras(bundle);
			startActivity(intent);
			finish();//结束该类
		}

		@Override
		public void onRoutePlanFailed() {
			// TODO Auto-generated method stub
			Toast.makeText(BNDemoMainActivity.this, "算路失败",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void initSetting() {
		// BNaviSettingManager.setDayNightMode(BNaviSettingManager.DayNightMode.DAY_NIGHT_MODE_DAY);
		BNaviSettingManager
				.setShowTotalRoadConditionBar(BNaviSettingManager.PreViewRoadCondition.ROAD_CONDITION_BAR_SHOW_ON);
		BNaviSettingManager.setVoiceMode(BNaviSettingManager.VoiceMode.Veteran);
		// BNaviSettingManager.setPowerSaveMode(BNaviSettingManager.PowerSaveMode.DISABLE_MODE);
		BNaviSettingManager
				.setRealRoadCondition(BNaviSettingManager.RealRoadCondition.NAVI_ITS_ON);
	}

	private BNOuterTTSPlayerCallback mTTSCallback = new BNOuterTTSPlayerCallback() {

		@Override
		public void stopTTS() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "stopTTS");
		}

		@Override
		public void resumeTTS() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "resumeTTS");
		}

		@Override
		public void releaseTTSPlayer() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "releaseTTSPlayer");
		}

		@Override
		public int playTTSText(String speech, int bPreempt) {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "playTTSText" + "_" + speech + "_" + bPreempt);

			return 1;
		}

		@Override
		public void phoneHangUp() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "phoneHangUp");
		}

		@Override
		public void phoneCalling() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "phoneCalling");
		}

		@Override
		public void pauseTTS() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "pauseTTS");
		}

		@Override
		public void initTTSPlayer() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "initTTSPlayer");
		}

		@Override
		public int getTTSState() {
			// TODO Auto-generated method stub
			Log.e("test_TTS", "getTTSState");
			return 1;
		}
	};

	public void onRequestPermissionsResult(int requestCode,
			String[] permissions, int[] grantResults) {
		// TODO Auto-generated method stub
		// super.onRequestPermissionsResult(requestCode, permissions,
		// grantResults);
		if (requestCode == authBaseRequestCode) {
			for (int ret : grantResults) {
				if (ret == 0) {
					continue;
				} else {
					Toast.makeText(BNDemoMainActivity.this, "缺少导航基本的权限!",
							Toast.LENGTH_SHORT).show();
					return;
				}
			}
			initNavi();
		} else if (requestCode == authComRequestCode) {
			for (int ret : grantResults) {
				if (ret == 0) {
					continue;
				}
			}
			routeplanToNavi(mCoordinateType);
		}

	}
}
